<?php

namespace Platform\Services\Pay;

use Platform\Services\BaseService;

class RewardPayService extends BaseService {

    /**
     * 
     * @param type $userId
     * @param type $objectType
     * @param type $objectId
     * @param type $cash
     * @param type $userInfo
     * @return type
     */
    public function createPayOrder($cash,$type) {
        $cash = doubleval($cash) ? doubleval($cash) : E("金额不能为空");
        $orderNo = $this->get_order_no();
        $createTime = time();
        $status = 0;
        $payOrderModel = model("Pay/RewardPayOrder");
        $payOrderModel->setOrderNo($orderNo);
        $payOrderModel->setStatus($status);
        $payOrderModel->setType($type);
        $payOrderModel->setCreateTime($createTime);
        $payOrderModel->setCash($cash);
        $payOrderModel->setUpdateTime(0);
        $result = $payOrderModel->save();
        if ($result == FALSE) {
            $messages = $payOrderModel->getMessages();
            foreach ($messages as $message) {
                E($message, "\n");
            }
        }
        return $payOrderModel->toArray();
    }

    /**
     * 查看订单 
     */
    public function getPayOrderInfoById($id) {
        $id = intval($id) ? intval($id) : E("订单id不能为空");
        $payOrderModel = model('Pay/RewardPayOrder');
        $payOrderInfo = $payOrderModel->getDataById($id);
        return $payOrderInfo;
    }

    /**
     * 查看订单 
     */
    public function getPayOrderByOrderNo($orderNo) {
        $orderNo = trim($orderNo) ? trim($orderNo) : E("订单号不能为空");
        $payOrderModel = model('Pay/RewardPayOrder');
        $payOrderInfo = $payOrderModel->findFirstByOrderNo($orderNo);
        if ($payOrderInfo) {
            $payOrderInfo = $payOrderInfo->toArray();
        } else {
            E("订单不存在");
        }
        return $payOrderInfo;
    }

    /**
     * 订单确认
     */
    public function payOrderSuccess($id, $openId) {
        $id = intval($id) ? intval($id) : E("订单id不能为空");
        $this->db->begin();
        $payOrderModel = model("Pay/RewardPayOrder");
        $payOrderInfoObject = $payOrderModel->getForUpdateDataById($id);
        if (empty($payOrderInfoObject)) {
            $this->db->rollback();
            E("订单不存在");
        }
        $payOrderInfo = $payOrderInfoObject->toArray();
        if ($payOrderInfo['status'] != 0) {
            $this->db->rollback();
            E("订单状态已确认");
        }
        $payOrderInfoObject->setUpdateTime(time());
        $payOrderInfoObject->setStatus(1);
        $payOrderInfoObject->setOpenId($openId);
        $result = $payOrderInfoObject->save();
        if ($result === FALSE) {
            $this->db->rollback();
            E("操作订单数据出错");
        }
        $this->db->commit();
        return $payOrderInfo;
    }

    /*
     * 得到订单号
     */

    public function get_order_no() {
        $date_code = array('0',
            '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
            'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y');
        $order_sn = date('y') .
                strtoupper(dechex(date('m'))) . $date_code[intval(date('d'))] .
                substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $order_sn;
    }

}

?>